import Vuex from 'vuex';
import Vue from 'vue';
const CREATE_LINE_ITEM = "CREATE_LINE_ITEM"; //初始化，给每一个的商品都是isSelected，为不选中
const PUSH_CURRENT_PRODUCT = "PUSH_CURRENT_PRODUCT"; //点击确定 无商品，把当前商品添加到购物车
const PUSH_CURRENT_PRODUCT_NUM = "PUSH_CURRENT_PRODUCT_NUM"; //已有商品，替换数量
const DELETE_CURRENT_PRODUCT = "DELETE_CURRENT_PRODUCT"; //删除当前商品
const CHECKOUT_SINGLE_PRODUCT = "CHECKOUT_SINGLE_PRODUCT"; //购物车中选中了一个商品
const CHECKOUT_SINGLE_PRODUCT_DELETE = "CHECKOUT_SINGLE_PRODUCT_DELETE"; //购物车不选中一个商品
const SELECT_LINE_ITEMS = "SELECT_LINE_ITEMS"; //全选
const UNSELECT_LINE_ITEMS = "UNSELECT_LINE_ITEMS"; //全不选
const CLEAR_CURRENT_PRODUCT_CAR = "CLEAR_CURRENT_PRODUCT_CAR"; //清除当前选中的的购物车
const ADD_LINE_ITEM = "ADD_LINE_ITEM"; //购物车中的加数量
const DECREASE_LINE_ITEM = "DECREASE_LINE_ITEM"; //购物车中的减去数量

const state = {
    currentProduct: [], //购物车中所有商品
}
const getters = {
    currentProductCar: (state) => state.currentProduct,
};

let createLineItem = (state, lineItem) => { //给每一个的商品都是isSelected，为不选中
    if(lineItem != undefined){
        lineItem.map((item) => {
            item.isSelected = false
        })
        state.currentProduct = lineItem
    }
}
let deleteCurrentGoods = (state, id) => {
    for (let i = 0; i < state.currentProduct.length; i++) {
        if (state.currentProduct[i].lineItemId === id) {
            state.currentProduct.splice(i, 1);
        }
    }
}
let chaneProductNum = (state, goods) => {
    state.currentProduct.map((item) => {
        if (item.lineItemId === goods.lineItemId) {
            Vue.set(item, "quantity", goods.quantity)
        }
    })
}
const mutations = {
    [CREATE_LINE_ITEM](state, lineItem) {
        createLineItem(state, lineItem)
    },
    [PUSH_CURRENT_PRODUCT](state, goods) {
        state.currentProduct.push(goods)
    },
    [PUSH_CURRENT_PRODUCT_NUM](state, goods) {
        chaneProductNum(state, goods)
    },
    [DELETE_CURRENT_PRODUCT](state, id) {
        deleteCurrentGoods(state, id)
    },
    [CHECKOUT_SINGLE_PRODUCT](state, id) {
        state.currentProduct.map((item) => {
            if (item.lineItemId === id) {
                Vue.set(item, 'isSelected', true)
            }
        })
    },
    [CHECKOUT_SINGLE_PRODUCT_DELETE](state, id) {
        state.currentProduct.map((item) => {
            if (item.lineItemId === id) {
                Vue.set(item, 'isSelected', false)
            }
        })
    },
    [SELECT_LINE_ITEMS](state) {
        state.currentProduct.map((item) => {
            Vue.set(item, 'isSelected', true)
        })
    },
    [UNSELECT_LINE_ITEMS](state) {
        state.currentProduct.map((item) => {
            Vue.set(item, 'isSelected', false)
        })
    },
    [CLEAR_CURRENT_PRODUCT_CAR](state, goodsAll) { //清除当前选中的的购物车
        for (let i = 0; i < state.currentProduct.length; i++) {
            for (let j = 0; j < goodsAll.length; j++) {
                if (state.currentProduct[i].lineItemId === goodsAll[j].lineItemId) {
                    state.currentProduct.splice(i, 1);
                }
            }
        }
    },
    [ADD_LINE_ITEM](state, goods) { //购物车中的加数量
        chaneProductNum(state, goods)
    },
    [DECREASE_LINE_ITEM](state, goods) { //购物车中的减数量
        chaneProductNum(state, goods)
    },
};
const actions = {
    createLineItem({ commit }, lineItem) {
        commit(CREATE_LINE_ITEM, lineItem)
    },
    pushCurrentProduct({ commit }, response) {
        commit(PUSH_CURRENT_PRODUCT, response)
    },
    pushCurrentProductNum({ commit }, lineItem) {
        commit(PUSH_CURRENT_PRODUCT_NUM, lineItem)
    },
    deleteCurrentProduct({ commit }, id) {
        commit(DELETE_CURRENT_PRODUCT, id)
    },
    checkSingleProduct({ commit }, id) {
        commit(CHECKOUT_SINGLE_PRODUCT, id)
    },
    checkSingleProductDelete({ commit }, id) {
        commit(CHECKOUT_SINGLE_PRODUCT_DELETE, id)
    },
    selectLineItems({ commit }) {
        commit(SELECT_LINE_ITEMS)
    },
    unSelectLineItems({ commit }) {
        commit(UNSELECT_LINE_ITEMS)
    },
    clearCurrentProductCar({ commit }, productAll) { //清除当前选中的的购物车
        commit(CLEAR_CURRENT_PRODUCT_CAR, productAll)
    },
    addLineItemQuantity({ commit }, goods) { //购物车中的加数量
        commit(ADD_LINE_ITEM, goods)
    },
    decreaseLineItemQuantity({ commit }, goods) { //购物车中的减数量
        commit(DECREASE_LINE_ITEM, goods)
    },
};
















export default {
    state,
    mutations,
    actions,
    getters
}